package com.aiti.lulian.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.entity.LuLianDockingMeetingRoom;
import com.aiti.lulian.service.ILuLianDockingMeetingRoomService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WongMZ
 * @createDate 2024-08-12
 * @description:
 */
@RestController
@RequestMapping("/meetingRoom")
@Api(tags = "luLianDockingMeetingRoomController", description = "luLianDockingMeetingRoom控制层")
public class LuLianDockingMeetingRoomController extends WebController {

    protected final Logger logger = LoggerFactory.getLogger(LuLianDockingMeetingRoomController.class);

    @Autowired
    private ILuLianDockingMeetingRoomService meetingRoomService;

    @ApiOperation(value = "新增线下活动直播间")
    @PostMapping
    public Message saveMeetingRoom(@RequestBody LuLianDockingMeetingRoom meetingRoom) {
        try {
            if (meetingRoom == null || BeanUtil.isEmpty(meetingRoom)) {
                logger.error("新增异常：===》入参异常");
                return Error("入参异常");
            }
            String userId = findUserId();
            if (StrUtil.isNotBlank(userId)) {
                meetingRoom.setCreator(userId);
            } else {
                System.out.println("------------------未注册------------------");
                return Fail("请先注册登录！");
            }
            //判断要关联的直播间ID是否已存在，若已存在，则提示“该直播间ID已关联”
            LambdaQueryWrapper<LuLianDockingMeetingRoom> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(LuLianDockingMeetingRoom::getRoomId, meetingRoom.getRoomId());
            LuLianDockingMeetingRoom queryRes = meetingRoomService.getOne(wrapper);
            if (queryRes != null) {
                return Fail("该直播间ID已关联！");
            }
            boolean res = meetingRoomService.save(meetingRoom);
            if (res) {
                return Success();
            } else {
                return Fail();
            }
        } catch (Exception e) {
            logger.error("新增异常：===》" + e);
            return Error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询线下活动与直播间关联列表")
    @GetMapping("/list")
    public Message queryMeetingRoomPageList(@RequestParam("pageNum") String pageNum,
                                            @RequestParam("pageSize") String pageSize) {
        try {
            if (StrUtil.isBlank(pageNum) || StrUtil.isBlank(pageSize)) {
                logger.error("查询异常：===》入参异常");
                return Error("入参异常");
            }
            Page<LuLianDockingMeetingRoom> page = meetingRoomService.page(new Page<>(Long.valueOf(pageNum), Long.valueOf(pageSize)));
            Map<String, Object> map = new HashMap<>();
            map.put("total", page.getTotal());
            map.put("list", page.getRecords());
            return Success(map);
        } catch (Exception e) {
            logger.error("查询异常：===》" + e);
            return Error(e.getMessage());
        }
    }

    @ApiOperation(value = "根据baseId删除关联")
    @DeleteMapping("/{baseId}")
    public Message deleteMeetingRoomByBaseId(@PathVariable("baseId") String baseId) {
        try {
            if (StrUtil.isBlank(baseId)) {
                logger.error("删除异常：===》入参异常");
                return Error("入参异常");
            }
            boolean res = meetingRoomService.removeById(baseId);
            if (res) {
                return Success();
            } else {
                return Fail();
            }
        } catch (Exception e) {
            logger.error("删除异常：===》" + e);
            return Error(e.getMessage());
        }
    }

    @ApiOperation(value = "根据baseId修改关联")
    @PutMapping
    public Message updateMeetingRoomByBaseId(@RequestBody LuLianDockingMeetingRoom meetingRoom) {
        try {
            if (meetingRoom == null || BeanUtil.isEmpty(meetingRoom)) {
                logger.error("更新异常：===》入参异常");
                return Error("入参异常");
            }
            String userId = findUserId();
            if (StrUtil.isNotBlank(userId)) {
                meetingRoom.setUpdateBy(userId);
            } else {
                System.out.println("------------------未注册------------------");
                return Fail("请先注册登录！");
            }
            LambdaUpdateWrapper<LuLianDockingMeetingRoom> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(LuLianDockingMeetingRoom::getBaseId, meetingRoom.getBaseId());
            wrapper.set(LuLianDockingMeetingRoom::getRoomId, meetingRoom.getRoomId());
            boolean res = meetingRoomService.update(wrapper);
            if (res) {
                return Success();
            } else {
                return Fail();
            }
        } catch (Exception e) {
            logger.error("更新异常：===》" + e);
            return Error(e.getMessage());
        }
    }
}